<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fn="http://www.w3.org/2003/11/xpath-functions">

	<xsl:output method="xml" encoding="UTF-8" indent="false" />

	<xsl:template match="/dict">
		<graph>

			<xsl:key name="album" match="track"
				use="string[@name = 'Album']" />

			<xsl:key name="genre" match="track"
				use="string[@name = 'Genre']" />

			<xsl:key name="genre_album" match="track"
				use="concat(string[@name = 'Genre'], string[@name = 'Album'])" />

			<xsl:key name="artist" match="track"
				use="string[@name = 'Artist' or @name = 'Album Artist' or @name = 'Composer']" />

			<xsl:variable name="start_node_id" select="'StartRootId'" />
			<node>
				<id>
					<xsl:value-of select="$start_node_id" />
				</id>
				<type>SmartiesStart</type>
				<label>Click on children !</label>
				<localityRadius>1</localityRadius>
				<emptyRingStyle>
					<color>
						<r>0</r>
						<g>0</g>
						<b>0</b>
						<a>0.2</a>
					</color>
				</emptyRingStyle>
			</node>

			<xsl:variable name="genre_root_id" select="'GenreRootId'" />
			<node>
				<id>
					<xsl:value-of select="$genre_root_id" />
				</id>
				<type>genre</type>
				<label>Genres</label>
				<localityRadius>1</localityRadius>
				<emptyRingStyle>
					<color>
						<r>0</r>
						<g>0</g>
						<b>0</b>
						<a>0.2</a>
					</color>
				</emptyRingStyle>
			</node>

			<edge>
				<from>
					<xsl:value-of select="$start_node_id" />
				</from>
				<to>
					<xsl:value-of select="$genre_root_id" />
				</to>
			</edge>

			<xsl:variable name="artiste_root_id"
				select="'ArtisteRootId'" />
			<node>
				<id>
					<xsl:value-of select="$artiste_root_id" />
				</id>
				<type>artiste</type>
				<label>Artistes</label>
				<localityRadius>1</localityRadius>
				<emptyRingStyle>
					<color>
						<r>0</r>
						<g>0</g>
						<b>0</b>
						<a>0.2</a>
					</color>
				</emptyRingStyle>
			</node>

			<edge>
				<from>
					<xsl:value-of select="$start_node_id" />
				</from>
				<to>
					<xsl:value-of select="$artiste_root_id" />
				</to>
			</edge>

			<xsl:variable name="album_root_id" select="'AlbumRootId'" />
			<node>
				<id>
					<xsl:value-of select="$album_root_id" />
				</id>
				<type>album</type>
				<label>Albums</label>
				<localityRadius>1</localityRadius>
				<emptyRingStyle>
					<color>
						<r>0</r>
						<g>0</g>
						<b>0</b>
						<a>0.2</a>
					</color>
				</emptyRingStyle>
			</node>

			<edge>
				<from>
					<xsl:value-of select="$start_node_id" />
				</from>
				<to>
					<xsl:value-of select="$album_root_id" />
				</to>
			</edge>

			<xsl:for-each select="//track">

				<xsl:variable name="id" select="generate-id(.)" />
				<xsl:variable name="track_id"
					select="concat('track', $id)" />

				<node>
					<id>
						<xsl:value-of select="$track_id" />
					</id>
					<type>track</type>
					<label>
						<xsl:value-of
							select="./string[@name = 'Name']/text()" />
					</label>
					<xsl:for-each
						select="./string[@name = 'Name' 
										or @name = 'Artist' 
										or @name = 'Album' 
										or @name = 'Genre' 
										or @name = 'Kind' 
										or @name = 'Track Type'
										or @name = 'Location'
										or @name = 'Grouping'
										or @name = 'Composer'
										or @name = 'Album Artist'
										 ]">
						<string>
							<key>
								<xsl:value-of select="@name" />
							</key>
							<value>
								<xsl:value-of select="text()" />
							</value>
						</string>
					</xsl:for-each>
					<xsl:for-each
						select="./integer[@name = 'Size' 
										or @name = 'Total Time'
										or @name = 'Disc Number'
										or @name = 'Disc Count'
										or @name = 'Track Number'
										or @name = 'Year'
										or @name = 'Bit Rate'
										or @name = 'Sample Rate'
										or @name = 'Artwork Count'
										or @name = 'Album Rating'
										or @name = 'Rating'
										]">
						<integer>
							<key>
								<xsl:value-of select="@name" />
							</key>
							<value>
								<xsl:value-of select="text()" />
							</value>
						</integer>
					</xsl:for-each>
					<xsl:for-each
						select="./date[@name = 'Date Modified' 
									or @name = 'Date Added'
									]">
						<date>
							<key>
								<xsl:value-of select="@name" />
							</key>
							<value>
								<xsl:value-of select="text()" />
							</value>
						</date>
					</xsl:for-each>

					<localityRadius>2</localityRadius>
					<smartStyle>
						<color>
							<r>230</r>
							<g>0</g>
							<b>230</b>
							<a>0.2</a>
						</color>
					</smartStyle>
				</node>

				<xsl:variable name="genre_id"
					select="concat('genre', $id)" />
				<xsl:variable name="genre_name"
					select="./string[@name = 'Genre']/text()" />
				<xsl:variable name="first_genre_id"
					select="concat('genre', generate-id(key('genre', $genre_name)[1]))" />
				<xsl:if test="$genre_id = $first_genre_id">

					<node>
						<id>
							<xsl:value-of select="$genre_id" />
						</id>
						<type>genre</type>
						<label>
							<xsl:value-of select="$genre_name" />
						</label>



						<localityRadius>1</localityRadius>
						<emptyRingStyle>
							<color>
								<r>128</r>
								<g>128</g>
								<b>50</b>
								<a>0.2</a>
							</color>
						</emptyRingStyle>


						<integer>
							<key>
								<xsl:value-of select="'trackCount'" />

							</key>
							<value>
								<xsl:value-of
									select="count(key('genre', $genre_name))" />
							</value>
						</integer>

						<info>
							<xsl:value-of select="'trackCount'" />
						</info>

					</node>

					<edge>
						<from>
							<xsl:value-of select="$genre_root_id" />
						</from>
						<to>
							<xsl:value-of select="$genre_id" />
						</to>
					</edge>

				</xsl:if>

				<xsl:variable name="album_id"
					select="concat('album', $id)" />
				<xsl:variable name="album_name"
					select="./string[@name = 'Album']/text()" />
				<xsl:variable name="first_album_id"
					select="concat('album', generate-id(key('album', $album_name)[1]))" />
				<xsl:if test="$album_id = $first_album_id">

					<node>
						<id>
							<xsl:value-of select="$album_id" />
						</id>
						<type>album</type>
						<label>
							<xsl:value-of select="$album_name" />
						</label>

						<localityRadius>1</localityRadius>
						<circleStyle>
							<color>
								<r>255</r>
								<g>150</g>
								<b>12</b>
								<a>0.2</a>
							</color>
						</circleStyle>

						<integer>
							<key>
								<xsl:value-of select="'trackCount'" />
							</key>
							<value>
								<xsl:value-of
									select="count(key('album', $album_name))" />
							</value>
						</integer>

						<info>
							<xsl:value-of select="'trackCount'" />
						</info>
					</node>

					<edge>
						<from>
							<xsl:value-of select="$album_root_id" />
						</from>
						<to>
							<xsl:value-of select="$album_id" />
						</to>
					</edge>

				</xsl:if>

				<xsl:if test="not($first_album_id = 'album')">
					<edge>
						<from>
							<xsl:value-of select="$first_album_id" />
						</from>
						<to>
							<xsl:value-of select="$track_id" />
						</to>
					</edge>

					<xsl:variable name="first_genre_album_id"
						select="concat('genre', generate-id(key('genre_album', concat($genre_name, $album_name))[1]))" />

					<xsl:if
						test="$genre_id = $first_genre_album_id and not($first_genre_id = 'genre')">
						<edge>
							<from>
								<xsl:value-of select="$first_genre_id" />
							</from>
							<to>
								<xsl:value-of select="$first_album_id" />
							</to>
						</edge>
					</xsl:if>
				</xsl:if>

				<xsl:variable name="artist_id"
					select="concat('artist', $id)" />
				<xsl:variable name="artist_name"
					select="./string[@name = 'Artist']/text()" />
				<xsl:variable name="first_artist_id"
					select="concat('artist', generate-id(key('artist', $artist_name)[1]))" />

				<xsl:if test="$artist_id = $first_artist_id">
					<node>
						<id>
							<xsl:value-of select="$artist_id" />
						</id>
						<type>artist</type>
						<label>
							<xsl:value-of select="$artist_name" />
						</label>


						<localityRadius>1</localityRadius>
						<emptyRingStyle>
							<color>
								<r>130</r>
								<g>180</g>
								<b>220</b>
								<a>0.2</a>
							</color>
						</emptyRingStyle>


						<integer>
							<key>
								<xsl:value-of select="'trackCount'" />

							</key>
							<value>
								<xsl:value-of
									select="count(key('artist', $artist_name))" />
							</value>
						</integer>

						<info>
							<xsl:value-of select="'trackCount'" />
						</info>
					</node>

					<edge>
						<from>
							<xsl:value-of select="$artiste_root_id" />
						</from>
						<to>
							<xsl:value-of select="$artist_id" />
						</to>
					</edge>

				</xsl:if>

				<xsl:variable name="test_track_count_for_artist"
					select="count(key('artist', $artist_name)[string[@name = 'Album'] = $album_name]) = count(key('album', $album_name))" />

				<xsl:if
					test="not($first_artist_id = 'artist') and not($test_track_count_for_artist)">
					<edge>
						<from>
							<xsl:value-of select="$first_artist_id" />
						</from>
						<to>
							<xsl:value-of select="$track_id" />
						</to>
					</edge>
				</xsl:if>

				<xsl:if
					test="not($first_artist_id = 'artist') and $test_track_count_for_artist and not($first_album_id = 'album') and $album_id = $first_album_id">
					<edge>
						<from>
							<xsl:value-of select="$first_artist_id" />
						</from>
						<to>
							<xsl:value-of select="$first_album_id" />
						</to>
					</edge>
				</xsl:if>

				<xsl:variable name="album_artist_name"
					select="./string[@name = 'Album Artist']/text()" />
				<xsl:variable name="first_album_artist_id"
					select="concat('artist', generate-id(key('artist', $album_artist_name)[1]))" />
				<xsl:if
					test="$artist_id = $first_album_artist_id and not($artist_name = $album_artist_name)">

					<node>
						<id>
							<xsl:value-of select="$artist_id" />
						</id>
						<type>artist</type>
						<label>
							<xsl:value-of select="$album_artist_name" />
						</label>

						<localityRadius>1</localityRadius>
						<emptyRingStyle>
							<color>
								<r>130</r>
								<g>180</g>
								<b>220</b>
								<a>0.2</a>
							</color>
						</emptyRingStyle>
					</node>

					<edge>
						<from>
							<xsl:value-of select="$artiste_root_id" />
						</from>
						<to>
							<xsl:value-of select="$artist_id" />
						</to>
					</edge>

				</xsl:if>

				<xsl:if
					test="not($first_album_id = 'album') and not($first_album_artist_id = 'artist') and $album_id = $first_album_id">
					<edge>
						<from>
							<xsl:value-of
								select="$first_album_artist_id" />
						</from>
						<to>
							<xsl:value-of select="$first_album_id" />
						</to>
					</edge>
				</xsl:if>

				<xsl:variable name="composer_name"
					select="./string[@name = 'Composer']/text()" />
				<xsl:variable name="first_composer_id"
					select="concat('artist', generate-id(key('artist', $composer_name)[1]))" />
				<xsl:if
					test="$artist_id = $first_composer_id and not($album_artist_name = $composer_name)">

					<node>
						<id>
							<xsl:value-of select="$artist_id" />
						</id>
						<type>artist</type>
						<label>
							<xsl:value-of select="$composer_name" />
						</label>

						<localityRadius>1</localityRadius>
						<emptyRingStyle>
							<color>
								<r>130</r>
								<g>180</g>
								<b>220</b>
								<a>0.2</a>
							</color>
						</emptyRingStyle>
					</node>

					<edge>
						<from>
							<xsl:value-of select="$artiste_root_id" />
						</from>
						<to>
							<xsl:value-of select="$artist_id" />
						</to>
					</edge>

				</xsl:if>

				<xsl:if test="not($first_composer_id = 'artist')">
					<edge>
						<from>
							<xsl:value-of select="$first_composer_id" />
						</from>
						<to>
							<xsl:value-of select="$track_id" />
						</to>
					</edge>
				</xsl:if>

			</xsl:for-each>
		</graph>
	</xsl:template>

</xsl:stylesheet>